package com.hzw.saas.common.quartz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.quartz.model.ScheduleJob;
import com.hzw.saas.common.quartz.service.IScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@Api(tags = "定时任务")
@RestController
@RequestMapping("/sys/schedule")
@RequiredArgsConstructor
public class ScheduleJobController {
	private final IScheduleJobService scheduleJobService;

	@GetMapping("/page")
    @ApiOperation(value = "定时任务列表")
	public ResponseEntity<IPage<ScheduleJob>> page(String beanName, PageParam page){
		IPage<ScheduleJob> scheduleJobs = scheduleJobService.page(page.convert(),
            new LambdaQueryWrapper<ScheduleJob>()
                .like(StrUtil.isNotBlank(beanName),ScheduleJob::getBeanName,beanName));
		return ResponseEntity.ok(scheduleJobs);
	}

	@GetMapping("/info/{jobId}")
    @ApiOperation(value = "定时任务信息")
	public ResponseEntity<ScheduleJob> info(@PathVariable("jobId") Long jobId){
		ScheduleJob schedule = scheduleJobService.getById(jobId);
		return ResponseEntity.ok(schedule);
	}

	@PostMapping
    @ApiOperation(value = "保存定时任务")
	public ResponseEntity<Void> save(@RequestBody @Valid ScheduleJob scheduleJob){
		int dbAlikeCount = scheduleJobService.count(new LambdaQueryWrapper<ScheduleJob>().eq(ScheduleJob::getBeanName, scheduleJob.getBeanName()).eq(ScheduleJob::getMethodName, scheduleJob.getMethodName()));
        AssertUtil.assertThrow("定时任务已存在", dbAlikeCount > 0);

		scheduleJobService.saveAndStart(scheduleJob);
		return ResponseEntity.ok().build();
	}

	@PutMapping
    @ApiOperation(value = "修改定时任务")
	public ResponseEntity<Void> update(@RequestBody @Valid ScheduleJob scheduleJob){
		int dbAlikeCount = scheduleJobService.count(new LambdaQueryWrapper<ScheduleJob>().eq(ScheduleJob::getBeanName, scheduleJob.getBeanName()).eq(ScheduleJob::getMethodName, scheduleJob.getMethodName()).ne(ScheduleJob::getJobId,scheduleJob.getJobId()));
        AssertUtil.assertThrow("定时任务已存在", dbAlikeCount > 0);

		scheduleJobService.updateScheduleJob(scheduleJob);
		return ResponseEntity.ok().build();
	}

	@DeleteMapping
    @ApiOperation(value = "删除定时任务")
	public ResponseEntity<Void> delete(@RequestBody Long[] jobIds){
		scheduleJobService.deleteBatch(jobIds);
		return ResponseEntity.ok().build();
	}

	@PostMapping("/run")
    @ApiOperation(value = "立即执行任务")
	public ResponseEntity<Void> run(@RequestBody Long[] jobIds){
		scheduleJobService.run(jobIds);
		return ResponseEntity.ok().build();
	}

	@PostMapping("/pause")
    @ApiOperation(value = "暂停定时任务")
	public ResponseEntity<Void> pause(@RequestBody Long[] jobIds){
		scheduleJobService.pause(jobIds);
		return ResponseEntity.ok().build();
	}

	@PostMapping("/resume")
    @ApiOperation(value = "恢复定时任务")
	public ResponseEntity<Void> resume(@RequestBody Long[] jobIds){
		scheduleJobService.resume(jobIds);
		return ResponseEntity.ok().build();
	}

}
